package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.core.common.node.ZTreeNode;
import cn.stylefeng.guns.fremework.BaseController;
import cn.stylefeng.guns.fremework.ResponseData;
import cn.stylefeng.guns.modular.system.entity.SysRole;
import cn.stylefeng.guns.modular.system.entity.User;
import cn.stylefeng.guns.modular.system.service.SysRoleService;
import cn.stylefeng.guns.modular.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    private static String PREFIX = "/modular/system/sysRole";


    @Autowired
    private SysRoleService roleService;

    @Autowired
    private UserService userService;


    ///////////////////////分页///////////////////////
    @RequestMapping("")
    public String index() {
        return PREFIX + "/sysRole.html";
    }

    @ResponseBody
    @RequestMapping(value = "/page")
    public ResponseData page(Page page, SysRole params) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getName())){
            queryWrapper.and(wrapper -> wrapper.like(SysRole::getName, params.getName()));
        }
        IPage result = roleService.page(page, queryWrapper);
        return ResponseData.success(result);
    }
    ///////////////////////新增///////////////////////
    @ResponseBody
    @RequestMapping(value = "/list")
    public ResponseData list(SysRole params) {
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(params.getName())){
            queryWrapper.and(wrapper -> wrapper.like(SysRole::getName, params.getName()));
        }
        List<SysRole> result = roleService.list(queryWrapper);
        return ResponseData.success(result);
    }
    ///////////////////////新增///////////////////////
    @RequestMapping("/sysRoleAdd")
    public String sysRoleAdd() {
        return PREFIX + "/sysRoleAdd.html";
    }

    @ResponseBody
    @PostMapping("/save")
    public ResponseData save(SysRole params) {
        roleService.save(params);
        return ResponseData.success();
    }
    ///////////////////////修改///////////////////////
    @ResponseBody
    @PostMapping("/detail/{roleId}")
    public ResponseData detail(@PathVariable Long roleId) {
        SysRole result = this.roleService.getById(roleId);
        return ResponseData.success(result);
    }
    ///////////////////////修改///////////////////////
    @RequestMapping("/sysRoleEdit")
    public ModelAndView sysRoleEdit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(PREFIX + "/sysRoleEdit.html");
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/update")
    public ResponseData update(SysRole params) {
        roleService.updateById(params);
        return ResponseData.success();
    }
    ///////////////////////删除///////////////////////
    @ResponseBody
    @PostMapping("/remove/{roleId}")
    public ResponseData remove(@PathVariable Long roleId) {
        roleService.removeById(roleId);
        //TODO 删除角色相关权限
        return ResponseData.success();
    }







    @RequestMapping(value = "/role_assign/{roleId}")
    public String roleAssign(@PathVariable("roleId") Long roleId, Model model) {
        model.addAttribute("roleId", roleId);
        return PREFIX + "/role_assign.html";
    }


    /**
     * 配置权限
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping("/setAuthority")
    @ResponseBody
    public cn.stylefeng.roses.core.reqres.response.ResponseData setAuthority(@RequestParam("roleId") Long roleId, @RequestParam("ids") String ids) {
        this.roleService.setAuthority(roleId, ids);
        return SUCCESS_TIP;
    }

    /**
     * 获取角色列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping(value = "/roleTreeList")
    @ResponseBody
    public List<ZTreeNode> roleTreeList() {
        List<ZTreeNode> roleTreeList = this.roleService.roleTreeList();
//        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }

    /**
     * 获取角色列表，通过用户id
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @RequestMapping(value = "/roleTreeListByUserId/{userId}")
    @ResponseBody
    public List<ZTreeNode> roleTreeListByUserId(@PathVariable Long userId) {
        User theUser = userService.getById(userId);
        String roleId = theUser.getRoleId();
        if (StringUtils.isEmpty(roleId)) {
            return this.roleService.roleTreeList();
        } else {
            String[] strArray = roleId.split(",");
            //转化成Long[]
            Long[] longArray = new Long[strArray.length];
            for (int i = 0; i < strArray.length; i++) {
                longArray[i] = Long.valueOf(strArray[i]);
            }
            return this.roleService.roleTreeListByRoleId(longArray);
        }
    }



}
